{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fbea44ee",
   "metadata": {},
   "source": [
    "# Notebook基础教程"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02e99e94",
   "metadata": {},
   "source": [
    "## Jupyter Notebook安装下载 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87ff31de",
   "metadata": {},
   "source": [
    "## Jupyter Notebook 简介"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d891ed0",
   "metadata": {},
   "source": [
    "Anaconda是安装Jupyter Notebook的最佳方式。安装完成之后，启动Anaconda的Navigator，并启动Notebook，呈现如下界面：\n",
    "![图片](Image/图片1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f968c2e6",
   "metadata": {},
   "source": [
    "## 创建一个新的Notebook "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48d77574",
   "metadata": {},
   "source": [
    "新建一个Notebook Python 3 (ipykernel)，生成了一个Untitled.ipynb文件。\n",
    ".ipynb文件即所谓的一个Notebook，实际是基于JSON格式的文本文件，并且包含元数据(“Edit > Edit Notebook Metadata”)。\n",
    "新建的Notebook的界面大致如下：\n",
    "![图片](Image/图片2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08afac7e",
   "metadata": {},
   "source": [
    "试着运行一些python3程序\n",
    "![图片](Image/图片3.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adb55858",
   "metadata": {},
   "source": [
    "## 简单的Python程序的例子"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1da7347a",
   "metadata": {},
   "source": [
    "1. 定义selection_sort函数执行选择排序功能.\n",
    "2. 定义test函数进行测试，执行数据输入，并调用selection_sort函数进行排序，最后输出结果\n",
    "\n",
    "```\n",
    "def selection_sort(array):\n",
    "    for i in range(len(array)-1):\n",
    "        min_index = i\n",
    "        for j in range(i+1, len(array)):\n",
    "            if array[j] < array[min_index]:\n",
    "                min_index = j\n",
    "        if min_index != i:\n",
    "            array[i], array[min_index] = array[min_index], array[i]\n",
    "return array\n",
    "\n",
    "array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]\n",
    "print(selection_sort(array))\n",
    "```\n",
    "运行结果如下：\n",
    "![图片](Image/图片5.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8731cd2",
   "metadata": {},
   "source": [
    "## Python数据分析例子"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0d0c365",
   "metadata": {},
   "source": [
    "分析历年财富世界500强的数据(1955-2005)，可从[此处下载](https://www.jianguoyun.com/p/DabvAJEQ7JmuCRjI1LwEIAA)。\n",
    "\n",
    "导入相关的工具库\n",
    "\n",
    "```\n",
    "%matplotlib inline\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "```\n",
    "加载数据集\n",
    "```\n",
    "df = pd.read_csv('fortune500.csv')\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fa69acc",
   "metadata": {},
   "source": [
    "## 检查数据集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89c37541",
   "metadata": {},
   "source": [
    "![图片](Image/图片6.png)\n",
    "![图片](Image/图片7.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c70e6fbf",
   "metadata": {},
   "source": [
    "数据属性列进行重命名\n",
    "```\n",
    "df.columns = ['year', 'rank', 'company', 'revenue', 'profit']\n",
    "```\n",
    "检查数据条目是否加载完整\n",
    "```\n",
    "len(df)\n",
    "```\n",
    "检查属性列的类型\n",
    "```\n",
    "df.dtypes\n",
    "```\n",
    "其他属性列都正常，但是对于profit属性，期望的结果是float类型，因此其可能包含非数字的值，利用正则表达式进行检查。\n",
    "```\n",
    "non_numberic_profits = df.profit.str.contains('[^0-9.-]')\n",
    "df.loc[non_numberic_profits].head()\n",
    "```\n",
    "确实存在这样的记录，profit这一列为字符串，统计一下到底存在多少条这样的记录\n",
    "```\n",
    "len(df.profit[non_numberic_profits])\n",
    "```\n",
    "具体操作如下图所示\n",
    "![图片](Image/图片8.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39c5193b",
   "metadata": {},
   "source": [
    "总体来说，利润（profit）列包含非数字的记录相对来说较少。更进一步，使用直方图显示一下按照年份的分布情况。\n",
    "```\n",
    "bin_sizes, _, _ = plt.hist(df.year[non_numberic_profits], bins=range(1955, 2006))\n",
    "```\n",
    "![图片](Image/图片9.png)\n",
    "可见，单独年份这样的记录数都少于25条，即少于4%的比例。这在可以接受的范围内，因此删除这些记录。\n",
    "```\n",
    "df = df.loc[~non_numberic_profits]\n",
    "df.profit = df.profit.apply(pd.to_numeric)\n",
    "\n",
    "```\n",
    "再次检查数据记录的条目数和类型。\n",
    "```\n",
    "len(df)\n",
    "\n",
    "df.dtypes\n",
    "\n",
    "```\n",
    "\n",
    "![图片](Image/图片10.png)\n",
    "可见，上述操作已经达到清洗无效数据记录的效果。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ece25d5",
   "metadata": {},
   "source": [
    "## 使用matplotlib进行绘图\n",
    "以年分组绘制平均利润曲线。首先定义变量和方法。\n",
    "```\n",
    "group_by_year = df.loc[:, ['year', 'revenue', 'profit']].groupby('year')\n",
    "avgs = group_by_year.mean()\n",
    "x = avgs.index\n",
    "y1 = avgs.profit\n",
    "def plot(x, y, ax, title, y_label):\n",
    "    ax.set_title(title)\n",
    "    ax.set_ylabel(y_label)\n",
    "    ax.plot(x, y)\n",
    "    ax.margins(x=0, y=0)\n",
    "```\n",
    "接着进行绘图\n",
    "```\n",
    "fig, ax = plt.subplots()\n",
    "plot(x, y1, ax, 'Increase in mean Fortune 500 company profits from 1955 to 2005', 'Profit (millions)')\n",
    "```\n",
    "![图片](Image/图片11.png)\n",
    "\n",
    "接着绘制收入曲线\n",
    "```\n",
    "y2 = avgs.revenue\n",
    "fig, ax = plt.subplots()\n",
    "plot(x, y2, ax, 'Increase in mean Fortune 500 company revenues from 1955 to 2005', 'Revenue (millions)')\n",
    "```\n",
    "![图片](Image/图片12.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a67758d",
   "metadata": {},
   "source": [
    "公司收入曲线并没有出现急剧下降，可能是由于财务会计的处理。对数据结果进行标准差处理。\n",
    "```\n",
    "def plot_with_std(x, y, stds, ax, title, y_label):\n",
    "    ax.fill_between(x, y - stds, y + stds, alpha=0.2)\n",
    "    plot(x, y, ax, title, y_label)\n",
    "fig, (ax1, ax2) = plt.subplots(ncols=2)\n",
    "title = 'Increase in mean and std Fortune 500 company %s from 1955 to 2005'\n",
    "stds1 = group_by_year.std().profit.values\n",
    "stds2 = group_by_year.std().revenue.values\n",
    "plot_with_std(x, y1.values, stds1, ax1, title % 'profits', 'Profit (millions)')\n",
    "plot_with_std(x, y2.values, stds2, ax2, title % 'revenues', 'Revenue (millions)')\n",
    "fig.set_size_inches(14, 4)\n",
    "fig.tight_layout()\n",
    "\n",
    "```\n",
    "![图片](Image/图片13.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8a643a2",
   "metadata": {},
   "source": [
    "## 安装Jupyter Notebook扩展工具"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5303a9e6",
   "metadata": {},
   "source": [
    "Jupter Notebook的扩展工具(extensions)可以提供丰富的附加功能，例如代码补全、内容目录、变量检查等。\n",
    "首先Anaconda Navigator中启动命令行终端\n",
    "![图片](Image/图片14.png)\n",
    "```\n",
    "在弹出的终端中依次输入下面4条命令，注意要耐心等待命令执行完成。\n",
    "\n",
    "pip install jupyter_contrib_nbextensions\n",
    "\n",
    "jupyter contrib nbextension install --user\n",
    "\n",
    "pip install jupyter_nbextensions_configurator\n",
    "\n",
    "jupyter nbextensions_configurator enable --user\n",
    "\n",
    "```\n",
    "等待最后一条指令完成即可。完成之后，重新打开Jupyter Notebook启动页面，可以看到:\n",
    "\n",
    "![图片](Image/图片4.png)\n",
    "点击Nbextensions标签，勾选Hinterland即可：\n",
    "\n",
    "![图片](Image/图片15.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
